(function(undefined){

/* Simple JavaScript Inheritance
 * By John Resig http://ejohn.org/
 * MIT Licensed.
 */
// Inspired by base2 and Prototype
(function(){
  var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;

  // The base Class implementation (does nothing)
  this.Class = function(){};
 
  // Create a new Class that inherits from this class
  Class.extend = function(prop, statics) {
    statics = statics || {};
    var _super = this.prototype;
   
    // Instantiate a base class (but only create the instance,
    // don't run the init constructor)
    initializing = true;
    var prototype = new this();
    initializing = false;
   
    // Copy the properties over onto the new prototype
    for (var name in prop) {
      // Check if we're overwriting an existing function
      prototype[name] = typeof prop[name] == "function" &&
        typeof _super[name] == "function" && fnTest.test(prop[name]) ?
        (function(name, fn){
          return function() {
            var tmp = this._super;
           
            // Add a new ._super() method that is the same method
            // but on the super-class
            this._super = _super[name];
           
            // The method only need to be bound temporarily, so we
            // remove it when we're done executing
            var ret = fn.apply(this, arguments);       
            this._super = tmp;
           
            return ret;
          };
        })(name, prop[name]) :
        prop[name];
    }
   
    // The dummy class constructor
    function Class() {
      // All construction is actually done in the init method
      if ( !initializing && this.init )
        this.init.apply(this, arguments);
    }
   
    // Populate our constructed prototype object
    Class.prototype = prototype;
   
    // Enforce the constructor to be what we expect
    Class.constructor = Class;

    // And make this class extendable
    Class.extend = arguments.callee;
    
    for (prop in statics) Class[prop] = statics[prop];
    
    return Class;
  };
})(); // end Class

})(); // end module

// "Aspect oriented programming" host function generator.
// @param scope
// @param args - the arguments from the host function's invocation.
var AOP = function(host_fn) {
    
    var _attrs = {
        // Attach an event callback to a function to also fire when it fires.
        // @param cb - the callback function.
        // @param when - defaults to "after". if set to "before", cb is called before the host.
        on: function(opts){
            if ($.isFunction(opts)) return this.after(opts);
            this._reg = this._reg || {before:[],after:[]};
            this._reg[opts.when || 'after'].push({
                cb:opts.cb,
                scope:opts.scope
            });
        },
        
        before: function(cb) {
            this.on({
                when:'before',
                cb:cb
            });
        },
        
        after: function(cb) {
            this.on({
                when:'after',
                cb:cb
            });
        }
    };
    
    var f = function() {
        var _args = arguments, _this=this, i,
            _r = arguments.callee._reg = arguments.callee._reg || {before:[],after:[]};
        
        for(i=0; i<_r.before.length; i++) {
            _r.before[i].cb.apply(_r.before[i].scope || _this, _args);
        };

        var rv = host_fn.apply(this,arguments);
        
        for(i=0; i<_r.after.length; i++) {
            _r.after[i].cb.apply(_r.after[i].scope || _this, _args);
        };
        return rv;
    };
    for (var k in _attrs) { f[k] = _attrs[k]; }
    return f;
}
